package DataAnalysis_hzl

import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._

import java.util.Properties

object t6 {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .master("local[*]")
      .appName("数据分析第一题")
      .enableHiveSupport()
      .getOrCreate()


    // 准备连接mysql的配置
    val conn = new Properties()
    conn.setProperty("user", "root")
    conn.setProperty("password", "123456")
    conn.setProperty("driver", "com.mysql.jdbc.Driver")

    //  todo 读取已经处理好的数据
    val data = spark.read
      .jdbc("jdbc:mysql://192.168.40.110:3306/hzl?useSSL=false", "clean", conn)


    //  todo 分析二手车年龄占比
    val r1 = data.withColumn(
      "age",
      concat((lit(2025) - col("production_year").cast("int")).cast("string"),lit("年"))
    )
      .groupBy("age")
      .count()
      .distinct()

    r1.show

    r1.write.mode("overwrite")
      .jdbc("jdbc:mysql://192.168.40.110:3306/hzl?useSSL=false", "r6", conn)


    spark.close()
  }

}
